﻿using System.Collections.Generic;
using System.Web.Mvc;
using HawaiRestaurant.Models.DataContext;
using HawaiRestaurant.Models.Repositories;

namespace HawaiRestaurant.Controllers
{
    public class AdminController : Controller
    {
        #region Private Methods

        private readonly IHawaiiRestaurantRepository _hawaiiRestaurantRepository;

        #endregion

        #region Constructor

        public AdminController()
            : this(new HawaiiRestaurantRepository())
        {
        }


        public AdminController(IHawaiiRestaurantRepository repository)
        {
            _hawaiiRestaurantRepository = repository;
        }

        #endregion

        #region Position Management

        public ActionResult ListPosition()
        {
            IEnumerable<Position> positions = _hawaiiRestaurantRepository.FetchAllPosition();
            return View(positions);
        }

        public ActionResult CreatePosition()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePosition(Position position)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddPosition(position);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListPosition");
            }
            return View("Error");
        }

        public ActionResult DeletePosition(int id)
        {
            Position position = _hawaiiRestaurantRepository.GetPosition(id);
            if (position == null)
                return View("Error");
            return View(position);
        }

        [HttpPost]
        public ActionResult DeletePosition(int id, string confirmbutton)
        {
            Position position = _hawaiiRestaurantRepository.GetPosition(id);
            if (position == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeletePosition(position);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListPosition");
        }

        public ActionResult EditPosition(int id)
        {
            Position position = _hawaiiRestaurantRepository.GetPosition(id);
            if (position == null)
                return View("Error");
            return View(position);
        }

        [HttpPost]
        public ActionResult EditPosition(int id, string confirmbutton)
        {
            Position position = _hawaiiRestaurantRepository.GetPosition(id);
            if (position == null)
                return View("Error");

            UpdateModel(position);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListPosition");
        }

        #endregion

        #region Drink Type Management

        public ActionResult ListDinkTypes()
        {
            IEnumerable<DrinkType> drinkTypes = _hawaiiRestaurantRepository.FetchAllDrinkType();
            return View(drinkTypes);
        }

        public ActionResult CreateDinkType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateDinkType(DrinkType drinkType)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddDrinkType(drinkType);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListDinkTypes");
            }
            return View("Error");
        }

        public ActionResult DeleteDinkType(int id)
        {
            DrinkType drinkType = _hawaiiRestaurantRepository.GetDrinkType(id);
            if (drinkType == null)
                return View("Error");
            return View(drinkType);
        }

        [HttpPost]
        public ActionResult DeleteDinkType(int id, string confirmbutton)
        {
            DrinkType drinkType = _hawaiiRestaurantRepository.GetDrinkType(id);
            if (drinkType == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteDrinkType(drinkType);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListDinkTypes");
        }

        public ActionResult EditDinkType(int id)
        {
            DrinkType drinkType = _hawaiiRestaurantRepository.GetDrinkType(id);
            if (drinkType == null)
                return View("Error");
            return View(drinkType);
        }

        [HttpPost]
        public ActionResult EditDinkType(int id, string confirmbutton)
        {
            DrinkType drinkType = _hawaiiRestaurantRepository.GetDrinkType(id);
            if (drinkType == null)
                return View("Error");

            UpdateModel(drinkType);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListDinkTypes");
        }

        #endregion

        #region Food Type Management

        public ActionResult ListFoodTypes()
        {
            IEnumerable<FoodType> foodTypes = _hawaiiRestaurantRepository.FetchAllFoodType();
            return View(foodTypes);
        }

        public ActionResult CreateFoodType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateFoodType(FoodType foodType)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddFoodType(foodType);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListFoodTypes");
            }
            return View("Error");
        }

        public ActionResult DeleteFoodType(int id)
        {
            FoodType foodType = _hawaiiRestaurantRepository.GetFoodType(id);
            if (foodType == null)
                return View("Error");
            return View(foodType);
        }

        [HttpPost]
        public ActionResult DeleteFoodType(int id, string confirmbutton)
        {
            FoodType foodType = _hawaiiRestaurantRepository.GetFoodType(id);
            if (foodType == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteFoodType(foodType);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListFoodTypes");
        }

        public ActionResult EditFoodType(int id)
        {
            FoodType foodType = _hawaiiRestaurantRepository.GetFoodType(id);
            if (foodType == null)
                return View("Error");
            return View(foodType);
        }

        [HttpPost]
        public ActionResult EditFoodType(int id, string confirmbutton)
        {
            FoodType foodType = _hawaiiRestaurantRepository.GetFoodType(id);
            if (foodType == null)
                return View("Error");
            UpdateModel(foodType);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListFoodTypes");
        }

        #endregion

        #region Food Management

        public ActionResult ListFood()
        {
            IEnumerable<Food> food = _hawaiiRestaurantRepository.FetchAllFood();
            return View(food);
        }

        public ActionResult CreateFood()
        {
            ViewBag.FoodTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllFoodType(), "FoodTypeId", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateFood(Food food)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddFood(food);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListFood");
            }
            return View("Error");
        }

        public ActionResult DeleteFood(int id)
        {
            Food food = _hawaiiRestaurantRepository.GetFood(id);
            if (food == null)
                return View("Error");
            return View(food);
        }

        [HttpPost]
        public ActionResult DeleteFood(int id, string confirmbutton)
        {
            Food food = _hawaiiRestaurantRepository.GetFood(id);
            if (food == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteFood(food);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListFood");
        }

        public ActionResult EditFood(int id)
        {
            Food food = _hawaiiRestaurantRepository.GetFood(id);
            if (food == null)
                return View("Error");
            ViewBag.FoodTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllFoodType(), "FoodTypeId", "Name");
            return View(food);
        }

        [HttpPost]
        public ActionResult EditFood(int id, string confirmbutton)
        {
            Food food = _hawaiiRestaurantRepository.GetFood(id);
            if (food == null)
                return View("Error");
            UpdateModel(food);
            _hawaiiRestaurantRepository.Save();
            ViewBag.FoodTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllFoodType(), "FoodTypeId", "Name");
            return RedirectToAction("ListFood");
        }

        #endregion

        #region Drink Management

        public ActionResult ListDrink()
        {
            IEnumerable<Drink> drink = _hawaiiRestaurantRepository.FetchAllDrink();
            return View(drink);
        }

        public ActionResult CreateDrink()
        {
            ViewBag.DrinkTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllDrinkType(), "DrinkTypeId", "Name");
            return View();
        }

        [HttpPost]
        public ActionResult CreateDrink(Drink drink)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddDrink(drink);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListDrink");
            }
            return View("Error");
        }

        public ActionResult DeleteDrink(int id)
        {
            Drink drink = _hawaiiRestaurantRepository.GetDrink(id);
            if (drink == null)
                return View("Error");
            return View(drink);
        }

        [HttpPost]
        public ActionResult DeleteDrink(int id, string confirmbutton)
        {
            Drink drink = _hawaiiRestaurantRepository.GetDrink(id);
            if (drink == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteDrink(drink);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListDrink");
        }

        public ActionResult EditDrink(int id)
        {
            Drink drink = _hawaiiRestaurantRepository.GetDrink(id);
            if (drink == null)
                return View("Error");
            ViewBag.DrinkTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllDrinkType(), "DrinkTypeId", "Name");
            return View(drink);
        }

        [HttpPost]
        public ActionResult EditDrink(int id, string confirmbutton)
        {
            Drink drink = _hawaiiRestaurantRepository.GetDrink(id);
            if (drink == null)
                return View("Error");
            UpdateModel(drink);
            _hawaiiRestaurantRepository.Save();
            ViewBag.DrinkTypeId = new SelectList(_hawaiiRestaurantRepository.FetchAllDrinkType(), "DrinkTypeId", "Name");
            return RedirectToAction("ListFood");
        }

        #endregion

        #region Staff Manager

        public ActionResult ListStaff()
        {
            IEnumerable<Staff> staff = _hawaiiRestaurantRepository.FetchAllStaff();
            return View(staff);
        }

        public ActionResult CreateStaff()
        {
            return View();
        }

        public ActionResult Error()
        {
            return View("Error");
        }

        [HttpPost]
        public void CraeteUser(User userName)
        {
            if (ModelState.IsValid)
            {
                _hawaiiRestaurantRepository.AddUser(userName);
            }
            RedirectToAction("Error");
        }

        [HttpPost]
        public ActionResult CreateStaff(Staff staff)
        {
            if (ModelState.IsValid)
            {
                CraeteUser(staff.User);
                _hawaiiRestaurantRepository.AddStaff(staff);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("ListStaff");
            }
            return View("Error");
        }

        public ActionResult DeleteStaff(int id)
        {
            Staff staff = _hawaiiRestaurantRepository.GetStaff(id);
            if (staff == null)
                return View("Error");
            return View(staff);
        }

        [HttpPost]
        public ActionResult DeleteStaff(int id, string confirmbutton)
        {
            Staff staff = _hawaiiRestaurantRepository.GetStaff(id);
            if (staff == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteUser(staff.User);
            _hawaiiRestaurantRepository.DeleteStaff(staff);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListStaff");
        }

        public ActionResult EditStaff(int id)
        {
            Staff staff = _hawaiiRestaurantRepository.GetStaff(id);
            if (staff == null)
                return View("Error");

            return View(staff);
        }


        [HttpPost]
        public ActionResult EditStaff(int id, string confirmbutton)
        {
            Staff staff = _hawaiiRestaurantRepository.GetStaff(id);
            if (staff == null)
                return View("Error");
            UpdateModel(staff);
            _hawaiiRestaurantRepository.Save();

            return RedirectToAction("ListStaff");
        }

        #endregion

        #region Customer Manager

        public ActionResult ListCustomer()
        {
            IEnumerable<Customer> customers = _hawaiiRestaurantRepository.FetchAllCustomer();
            return View(customers);
        }

        public ActionResult CreateCustomer()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateCustomer(Customer customer)
        {
            if (ModelState.IsValid)
            {
                CraeteUser(customer.User);
                _hawaiiRestaurantRepository.AddCustomer(customer);
                _hawaiiRestaurantRepository.Save();
                return RedirectToAction("HomePageIndex","Home");
            }
            return View("Error");
        }

        public ActionResult DeleteCustomer(int id)
        {
            Customer customer = _hawaiiRestaurantRepository.GetCustomer(id);
            if (customer == null)
                return View("Error");
            return View(customer);
        }

        [HttpPost]
        public ActionResult DeleteCustomer(int id, string confirmbutton)
        {
            Staff staff = _hawaiiRestaurantRepository.GetStaff(id);
            if (staff == null)
                return View("Error");
            _hawaiiRestaurantRepository.DeleteUser(staff.User);
            _hawaiiRestaurantRepository.DeleteStaff(staff);
            _hawaiiRestaurantRepository.Save();
            return RedirectToAction("ListStaff");
        }

        public ActionResult EditCustomer(int id)
        {
            Customer customer = _hawaiiRestaurantRepository.GetCustomer(id);
            if (customer == null)
                return View("Error");

            return View(customer);
        }


        [HttpPost]
        public ActionResult EditCustomer(int id, string confirmbutton)
        {
            Customer customer = _hawaiiRestaurantRepository.GetCustomer(id);
            if (customer == null)
                return View("Error");
            UpdateModel(customer);
            _hawaiiRestaurantRepository.Save();

            return RedirectToAction("ListCustomer");
        }
        #endregion


        #region Others 
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Contact()
        {
            return View();
        }
        #endregion
    }
}